/*****************************************************************************
 *   Copyright 2005 Tim A Wang
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 *
 ****************************************************************************/
package com.jb.web.renderer;

import com.jb.web.bean.FormField;
import com.jb.web.context.WebContext;

import com.jb.faces.component.UIComponent;
import com.jb.faces.context.FacesContext;


public class FormItemRenderer extends DefaultRenderer {
    public void decode(FacesContext pCtx, UIComponent item) {

        FormField formField = (FormField)item;
        WebContext ctx = (WebContext)pCtx;
        String value = 
            ctx.getRequest().getParameter(formField.getFieldName(ctx));

        if (value != null)
            checkScript(value);

        if (formField.getMandatory() && 
            ((value == null) || (value.length() == 0))) {
            ctx.addErrorMessage(formField, 
                                "Please enter value for " + formField.getFieldName() + 
                                ".");
        } else {
            try {
                formField.setDecodedValue(value);
            } catch (Exception e) {
                ctx.addErrorMessage(formField, 
                                    "Failed to decode date value for " + 
                                    formField.getClientId() + 
                                    " make sure your format matches value  ");
            }

        }

    }

    public StringBuffer hiddenField(WebContext pCtx, FormField formField, 
                                    String value) {
        StringBuffer html = 
            new StringBuffer("<INPUT " + "name=\"" + formField.getFieldName(pCtx) + 
                             "\"");

        if (isEditable(pCtx, formField)) {
            html.append(addTagProperty(" type", "text", null))
                .append(getEditScript(pCtx, formField))
                .append(addTagProperty("style",formField.getStyle(pCtx),null))
                .append(addTagProperty("class", formField.getStyleClass(), null))
                .append(addTagProperty(" size", formField.getWidth(), null));
        } else {
            html.append(addTagProperty(" type", "hidden", null));
        }

        html.append(addTagProperty(" value", value, null)).append(">");

        return html;
    }

    protected String renderError(WebContext pCtx, FormField formField) {
        String str = pCtx.getErrorMessage(formField);

        if (str != null) {
            return "<div class=\"jbErrorMsg\">" + str + "</div>";
        } else {
            return "";
        }
    }
}
